package ext.tianma.part;



import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.part.WTPart;
import wt.part.WTPartDescribeLink;
import wt.part.WTPartHelper;
import wt.part.WTPartReferenceLink;
import wt.util.WTException;

public class PartRelatedDocUtil {
	public static void removeAllRelatedDocs( WTPart part ){
		try{
			removeDescribeDocs( part ) ;
			
			removeReferenceDocs( part ) ;
			
			removeCADDocs( part ) ;
		} catch (WTException e) {
			e.printStackTrace();
		}finally{
			
		}
	}
	
	public static void removeDescribeDocs( WTPart part ) throws WTException{
		//首先判断输入参数的有效性
		if	(part==null ){
			return ;
		}
		
		//得到和当前零件关联的文档的描述关系WTPartDescribeLink
		QueryResult qr= WTPartHelper.service.getDescribedByWTDocuments(part, false);     
		if( qr != null ) {
			while(qr.hasMoreElements()) {
				//将对象添加到
				Object obj= qr.nextElement();
				
				if( ( obj != null ) && ( obj instanceof WTPartDescribeLink ) ){
					
					WTPartDescribeLink partDescribeLink = ( WTPartDescribeLink ) obj ;
					
					PersistenceHelper.manager.delete( partDescribeLink ) ;
				}
			}
		}
	}
	
	public static void removeReferenceDocs( WTPart part ) throws WTException{
		//首先判断输入参数的有效性
		if	(part==null ){
			return ;
		}
		
		QueryResult qr= PersistenceHelper.manager.navigate(part, "references", WTPartReferenceLink.class, false);        
		while(qr !=null && qr.hasMoreElements()) {
			Object obj = qr.nextElement() ;
			
			if( obj != null && obj instanceof WTPartReferenceLink ){
				//得到link
				WTPartReferenceLink partReferenceLink = (WTPartReferenceLink) obj ;
				
				PersistenceHelper.manager.delete( partReferenceLink ) ;
			}
		}
	}
	
	public static void removeCADDocs( WTPart part ){
		
	}
}
